﻿using MyCommon.Helper;
using MySerilog.Sink;
using Serilog;
using Serilog.Sinks.PeriodicBatching;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MySerilog.Configuration
{
    public static class LogBatchingSinkConfiguration
    {
        /// <summary>
        /// 配置日志数据库
        /// </summary>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static LoggerConfiguration WriteToLogBatching(this LoggerConfiguration configuration)
        {
            if (!Appsettings.app("Appsettings", "LogToDb").ObjToBool())
            {
                return configuration;
            }

            var exampleSink = new LogBatchingSink();

            var batchingOptions = new PeriodicBatchingSinkOptions
            {
                BatchSizeLimit = 500,
                Period = TimeSpan.FromSeconds(10),
                EagerlyEmitFirstEvent = true,
                QueueLimit = 10000,
            };

            var batchingSink = new PeriodicBatchingSink(exampleSink, batchingOptions);

            return configuration.WriteTo.Sink(batchingSink);
        }
    }
}
